package com.nursinghome.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.nursinghome.bean.CategoryGoods;
import com.nursinghome.bean.Goods;
import com.nursinghome.bean.PageFilter;
import com.nursinghome.common.mapper.GoodsMapper;
import com.nursinghome.common.service.CategoryGoodsService;
import com.nursinghome.common.service.GoodsService;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
* @author 程硕
* @createDate 2025-04-07 01:05:46
*/
@Service
public class GoodsServiceImpl implements GoodsService{
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CategoryGoodsService categoryGoodsService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insert(Goods goods) throws NurisingHomeException {
        // 判断是否为同名商品
        Goods goodsById = goodsMapper.selectById(goods.getId());
        if (ObjectUtil.isNotEmpty(goodsById)){
            throw new NurisingHomeException("商品已存在");
        }
        /*
        * 判断添加的是否是菜品和服务
        * 1.货品
        *   1. 1正常添加流程
        * 2.菜品
        *   2.1 添加多个分类
        * */
        if (goods.getCategoryId() != null) {

            return goodsMapper.insert(goods) > 0;
        }else {
            // 添加多个分类
            goodsMapper.insert(goods);
            Integer goodsId = goods.getId();
            if (null == goods.getCategoryId()){
               return true;
            }
            for (Integer categoryId : goods.getCategoryIds()) {
                categoryGoodsService.insert(new CategoryGoods(null, categoryId, goodsId, null, null));
            }
        }
        return true;
    }

    @Override
    public Boolean update(Goods goods) throws NurisingHomeException {
        // 判断商品是否存在
        // 判断修改之后的商品是否存在
        Goods goodsById = goodsMapper.selectById(goods.getId());
        if (goodsById.getId() == goods.getId() && !ObjectUtil.isEmpty(goodsById)){
            List<Goods> list = goodsMapper.selectAll(new Goods() {{
                setName(goods.getName());
            }});
            Goods goodsByName = ObjectUtil.isEmpty(list) ? null : list.get(0);
            if (goodsByName != null &&
                    ObjectUtil.isNotEmpty(goodsByName) &&
                    !Objects.equals(goodsByName.getId(), goods.getId())) {
                throw new NurisingHomeException("商品已存在");
            }
            /*
             * 判断添加的是否是菜品和服务
             * 1.货品
             *   1. 1正常添加流程
             * 2.菜品
             *   2.1 添加多个分类
             * */

            if (goods.getCategoryId() != null) {
                if (!goods.getCategoryId().equals(-1)) {
                    return goodsMapper.update(goods) > 0;
                }else {
                    // 删除商品的所有分类
                    categoryGoodsService.deleteByGoodsId(goods.getId());
                    Integer goodsId = goods.getId();
                    for (Integer categoryId : goods.getCategoryIds()) {
                        categoryGoodsService.insert(new CategoryGoods(null, categoryId, goodsId, null, null));
                    }
                }
            }
            return goodsMapper.update(goods) > 0;
        }else {
            throw new NurisingHomeException("商品不存在");
        }
    }
    @Override
    public Boolean updateByCount(Goods goods) throws NurisingHomeException {

        return goodsMapper.update(goods) > 0;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Integer id) throws NurisingHomeException {
        // 判断是否有这个商品
        Goods goodsById = goodsMapper.selectById(id);
        if (ObjectUtil.isEmpty(goodsById)){
            throw new NurisingHomeException("商品不存在，无法删除");
        }
        // 删除对应表的信息
        categoryGoodsService.deleteByGoodsId(id);

        Goods goods = new Goods() {{
            setId(id);
            setIsDelete(0);
        }};
        return goodsMapper.update(goods) > 0;
    }

    @Override
    public Goods selectById(Integer id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public PageFilter<Goods> selectAll(String pageNum, String pageSize, Goods goods) {
        goods.setPageNum(pageNum);
        goods.setPageSize(pageSize);
        List<Goods> list = goodsMapper.selectAll(goods);

        return goods.getPageFilter(goods, list);
    }
}
